using UnityEngine;

public class Tool : MonoBehaviour
{
	public float health;

	private SpriteRenderer[] spriteRenderers;

	private Collider2D col;

	private bool colIsTrigger;

	[HideInInspector]
	public bool placeable = true;

	public bool onPlace;

	public bool placed;

	public bool damageable;

	public GameObject hitEffect;

	public ThownObject thrownObject;

	[HideInInspector]
	public string usedItemName;

	private Color onPlaceColor = new Color(1f, 1f, 1f, 0.7f);

	private int contactCount;

	public bool holdOnHand;

	private void Awake()
	{
		spriteRenderers = GetComponentsInChildren<SpriteRenderer>();
		col = GetComponent<Collider2D>();
		if ((bool)col)
		{
			colIsTrigger = col.isTrigger;
		}
	}

	private void FixedUpdate()
	{
		if (holdOnHand)
		{
			base.transform.position = CampaignGame.Instance.player.body.leftHandJoint.transform.position;
		}
	}

	public void Hurt(float damage)
	{
		if (damageable && placed)
		{
			if (!CampaignGame.Instance.isPlayground)
			{
				damage *= (float)Mathf.Max(DO.Campaign.day / 200, 1);
			}
			health -= damage;
			HitEffect();
			if (health <= 0f)
			{
				Die();
			}
		}
	}

	public void HitEffect()
	{
		if ((bool)hitEffect)
		{
			GameObject obj = Object.Instantiate(hitEffect, hitEffect.transform.position, Quaternion.identity);
			obj.SetActive(value: true);
			Object.Destroy(obj, 3f);
		}
	}

	public void Cancel()
	{
		Time.timeScale = 1f;
		GameObject.Find("Game").GetComponent<CampaignGame>().CancelPlaceTools();
	}

	public void OnPlace()
	{
		onPlace = true;
		if ((bool)col && !colIsTrigger)
		{
			col.isTrigger = true;
		}
		SpriteRenderer[] array = spriteRenderers;
		for (int i = 0; i < array.Length; i++)
		{
			array[i].color = onPlaceColor;
		}
	}

	public void Placed()
	{
		onPlace = false;
		placed = true;
		if ((bool)col && !colIsTrigger)
		{
			col.isTrigger = false;
		}
		SpriteRenderer[] array = spriteRenderers;
		for (int i = 0; i < array.Length; i++)
		{
			array[i].color = Color.white;
		}
	}

	public void Die()
	{
		for (int i = 0; i < DO.Campaign.stageItems.Count; i++)
		{
			if ((double)Vector2.Distance(DO.Campaign.stageItems[i].pos, base.transform.position) < 0.1)
			{
				DO.Campaign.availableItems.Remove(DO.Campaign.stageItems[i].name);
				if (DO.Campaign.stageItems[i].usedItemName != null)
				{
					DO.Campaign.availableItems.Remove(DO.Campaign.stageItems[i].usedItemName);
				}
				DO.Campaign.stageItems.RemoveAt(i);
				break;
			}
		}
		if (!CampaignGame.Instance.isPlayground && base.name == "Turret(Clone)")
		{
			foreach (Transform item in CampaignGame.Instance.itemListView.transform)
			{
				if (item.name == "Turret")
				{
					Object.Destroy(item.gameObject);
				}
			}
		}
		Object.Destroy(base.gameObject, 0.1f);
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		if (collision.gameObject.tag == "Ground" || collision.gameObject.tag == "Pet" || collision.gameObject.tag == "PlayerRestrictedArea" || placed)
		{
			return;
		}
		contactCount++;
		if (contactCount <= 0)
		{
			return;
		}
		placeable = false;
		if (col.isTrigger && onPlace)
		{
			SpriteRenderer[] array = spriteRenderers;
			for (int i = 0; i < array.Length; i++)
			{
				array[i].color = Color.red;
			}
		}
	}

	private void OnTriggerExit2D(Collider2D collision)
	{
		if (collision.gameObject.tag == "Ground" || collision.gameObject.tag == "Pet" || collision.gameObject.tag == "PlayerRestrictedArea" || placed)
		{
			return;
		}
		contactCount--;
		if (contactCount > 0)
		{
			return;
		}
		placeable = true;
		if (col.isTrigger && onPlace)
		{
			SpriteRenderer[] array = spriteRenderers;
			for (int i = 0; i < array.Length; i++)
			{
				array[i].color = onPlaceColor;
			}
		}
	}
}
